<?php
class ImageComponent extends Object {

	public $uploadDir = 'img';

	public function upload($file, $params) {

		if (is_uploaded_file($file['tmp_name'])) {

		}

	}

	public function get($url, $params) {

	}

	# $pathtofile, $type, $filesize, $width, $height, $new_width, $new_height, $filter
	function resize($p = array()) {
	
		$itype = next(explode('/', $p['type']));
		switch($itype) {
			case 'jpeg': $tail  = 'jpeg'; $quality = 100; break;
			case 'pjpeg': $tail  = 'jpeg'; $quality = 100; break;
			case 'png': $tail  = 'png';  $quality = 9; break;
			case 'x-png': $tail  = 'png'; $quality = 9; break;
			case 'gif': $tail  = 'gif'; $quality = 100; break;
			default: $tail = false; break;
		}

		if($tail && $p['new_width'] && $p['new_height']) {
			$imgcreate = 'imagecreatefrom'.$tail;
			$orig_image = $imgcreate($p['pathtofile']);

			$image_resized = imagecreatetruecolor($p['new_width'], $p['new_height']);
			imagecopyresized($image_resized, $orig_image, 0, 0, 0, 0, $p['new_width'], $p['new_height'], $p['width'], $p['height']);

			$imagei = 'image'.$tail;
			$imagei($image_resized, $p['pathtofile'], $quality);

			#filter image
			if (function_exists('imagefilter')) {
				if (isset($p['filter'])) {
					$orig_image = $imgcreate($p['pathtofile']);

					foreach ($p['filter'] as $filter) {
						$filterID = $this->getFilterID($filter);
						imagefilter($orig_image, $filterID);
					}
					$imagei($orig_image, $p['pathtofile'], $quality);
				}
			}

			$dataS['content'] = fread(fopen($p['pathtofile'], "r"), $p['filesize']);
			$dataS['size'] = $p['filesize'];
			$dataS['wh'] = $p['new_width'].'x'.$p['new_height'];
			return $dataS;
		}
	}

	function getRatioSize($w, $h, $max) {
		$rw = $rh = false;
		if ($w > $max) {
			$rw = $w / $max;
		}

		if ($h > $max) {
			$rh = $h / $max;
		}

		if ($rw || $rh) {
			if ($w > $h) {
				$ratio  = $w / $max;
				$h      = round($h/$ratio);
				return array($max, $h);
			} else {
				$ratio  = $h / $max;
				$w      = round($w/$ratio);
				return array($w, $max);
			}
		} else {
			return false;
		}
	}

	function getFilterID($name) {
		$filters['negate']		= 0;
		$filters['grayscale']		= 1;
		$filters['brightness']		= 2;
		$filters['contrast']		= 3;
		$filters['colorize']		= 4;
		$filters['edgedetect']		= 5;
		$filters['emboss']		= 6;
		$filters['gaussian_blur']	= 7;
		$filters['selective_blur']	= 8;
		$filters['mean_removal']	= 9;
		$filters['smooth']		= 10;
		$filters['pixelate']		= 11;

		if (isset($filters[$name])) {
			return $filters[$name];
		} else {
			return 0;
		}
	}

	function getImageForm($w, $h, $type = null) {
		
		if ($type) {
			$width	= 'width';
			$height = 'height';
		} else {
			$width	= 'long';
			$height = 'tail';
		}

		if ($w > $h) {
			return $width;
		} else {
			return $height;
		}	
	}

	public function getResizeSide($path = null) {
		if (!empty($path)) {
			$size = getimagesize($path);
			return $this->getImageForm($size[0], $size[1], 1);
		}
		return false;
	}

	function getAllowTypes() {
		return array('image/jpeg', 'image/pjpeg', 'image/png', 'image/x-png', 'image/gif');
	}
}
?>
